package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Date;

import javax.swing.JPanel;

@SuppressWarnings("serial")
class ClockPanel extends JPanel {
	public static final int pX = 0; // The x coordinate to display the current
									// time
	public static final int pY = 20; // The y coordinate to display the current
										// time
	private String time; // The current time

	public ClockPanel() {
		Date date = new Date();
		time = date.toString();
		Font textFont = new Font("SansSerif", Font.BOLD, 13);
		setFont(textFont);
		setForeground(new Color(0, 0, 0));
		setOpaque(false);
	}

	/**
	 * Display current time on panel
	 * 
	 * @param g
	 *            the Graphics object
	 */
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		String theTime = " " + time;
		g2.drawString(theTime, pX, pY);
	}

	/**
	 * Set the time to new current time
	 * 
	 * @param time
	 *            the new current time
	 */
	public void setTime(String time) {
		this.time = time;
	}

}

class ClockThread extends Thread {
	private ClockPanel panel;

	public ClockThread(ClockPanel thePanel) {
		panel = thePanel;
	}

	/**
	 * Loop forever updating current time
	 */
	public void run() {
		while (true) {
			Date date = new Date();
			panel.setTime(date.toString());
			panel.repaint();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
	}
}